<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/20
 * Time: 11:31
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Qq extends Base
{
    /**
     * 轮播图首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user_hkphone')->count();// 获取总条数
            $lists = Db::name('u_user_hkphone')
                ->page($Nowpage,$limits)
                ->order('id DESC')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加banner
     * @return mixed|\think\response\Json
     */
    public function bannerAdd(){
        if(Request::instance()->isPost()){
            $card = trim(input('card'), ' ');

            $have = Db::table('up_u_user_hkphone')
                ->where('card',$card)
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '有重复的身份证号了']);
            }else{
                $data = [
                    'card' => $card,
                    'time' => date('Y-m-d H:i:s')
                ];
                $res = Db::table('up_u_user_hkphone')->insert($data);
                if($res){
                    return json(['code' => 200, 'msg' => '添加成功']);
                }else{
                    return json(['code' => 100, 'msg' => '添加失败']);
                }
            }
        }
        return $this->fetch('qq/banneradd');
    }
    
    /**
     * 编辑轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerEdit(){
        if(Request::instance()->isPost()){
            $id = input('id');
            $card = trim(input('card'), ' ');
            $have = Db::table('up_u_user_hkphone')
                ->where('card',$card)
                ->where('id','<>',$id)
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '有重复的身份证号了']);
            }else{
                $data = [
                    'card' => $card,
                    'time' => date('Y-m-d H:i:s')
                ];
                $res = Db::table('up_u_user_hkphone')->where('id',$id)->update($data);
                if($res){
                    return json(['code' => 200, 'msg' => '修改成功']);
                }else{
                    return json(['code' => 100, 'msg' => '修改失败']);
                }
            }
        }
        $banner_id = input('banner_id');
        // banner
        $banner = Db::table('up_u_user_hkphone')
            ->where('id', $banner_id)
            ->find();
        $this->assign([
            'banner' => $banner
        ]);
        return $this->fetch('qq/banneredit');
    }
    /**
     * 编辑轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerDel(){
        $id = input('id');
        $res = Db::table('up_u_user_hkphone')->where('id',$id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}